﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace SysSUFront
{
    public partial class Window : Form
    {
        private byte agentCounter;

        public Window()
        {
            InitializeComponent();
            agentCounter = 1;
        }

        private void Window_Load(object sender, EventArgs e)
        {
            LogDock.Default.Show(dockPanel);
            setUbiBrokerLabel();
        }

        private void btnNewAgent_Click(object sender, EventArgs e)
        {
            var newAgentControl = new AgentControl() 
                { 
                  Text = String.Format("Agent {0}", agentCounter++)
                };

            newAgentControl.FormClosed += newAgentControl_FormClosed;

            newAgentControl.Show(dockPanel);
            btnRun.Enabled = btnClear.Enabled = true;

            Logger.Default.Debug(String.Format("{0} created", newAgentControl.Text));
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            new SettingsWindow().ShowDialog();
            setUbiBrokerLabel();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newAgentControl_FormClosed(object sender, FormClosedEventArgs e)
        {
            btnRun.Enabled = btnClear.Enabled = dockPanel.DocumentsCount > 1;
            Logger.Default.Debug(String.Format("{0} finalized", (sender as AgentControl).Text));
        }

        private void setUbiBrokerLabel()
        {
            if (!String.IsNullOrEmpty(Settings.Default.UbiCentreAddress) && Settings.Default.UbiCentrePort > 0)
            {
                lblUbiBroker.Text = String.Format(
                    "Using UbiBroker {0}:{1}", Settings.Default.UbiCentreAddress, Settings.Default.UbiCentrePort);
                Logger.Default.Info(String.Format(
                    "Using UbiBroker {0}:{1}", Settings.Default.UbiCentreAddress, Settings.Default.UbiCentrePort));
            }
            else
            {
                lblUbiBroker.Text = "No UbiBroker";
                Logger.Default.Warn("No UbiBroker");
            }
        }
    }
}
